﻿#include "parsexmlins.h"

ParsexmlIns::ParsexmlIns(QObject *parent)
    : QObject{parent}
{
    qDebug() << "ParsexmlIns::ParsexmlIns ---> construct begin.";
    auto res = std::async(std::launch::async, [this](){
        InitEventtypeMap();
    });
    res.get();
    qDebug() << "ParsexmlIns::ParsexmlIns ---> construct end.";
}
ParsexmlIns *ParsexmlIns::ins = nullptr;
ParsexmlIns *ParsexmlIns::getInstance()
{
    if(!ins){
        ins = new ParsexmlIns();
    }
    return ins;
}

void ParsexmlIns::InitEventtypeMap()
{
    TypetoInt_Map["events"] = addType::EVENT;
    TypetoInt_Map["agents"] = addType::Agent;
}

void ParsexmlIns::parseXml()
{
//    xmlcontent = getxmlcontent();
//    setcontentsToVector();
//    handlecontentsToMap();
//    handlemapToJsons();

    std::thread t([this](){
        openJsonFile(jsonData, error, doc, EVENT_JSON_PATH);
    });
    t.join();//jc
    Jsons.push_back(jsonData);
    jsonData.clear();
    openJsonFile(jsonData, error, doc, AGENT_JSON_PATH);//jc
    Jsons.push_back(jsonData);
}

map<QString, int> ParsexmlIns::getEventMap() const
{
    return TypetoInt_Map;
}

vector<QString> ParsexmlIns::getJsons() const
{
    return Jsons;
}

void ParsexmlIns::setRoot_map(map<QString, QStringList> &Root_map)
{
    this->Root_map = Root_map;
}

void ParsexmlIns::setRes_Map(map<int, QStringList> &Res_Map)
{
    this->Res_Map = Res_Map;
}

void ParsexmlIns::setTemPath_Map(map<QString, QString> &TemPath_Map)
{
    this->TemPath_Map = TemPath_Map;
}

void ParsexmlIns::setname(QString &name)
{
    _name = name;
}

map<QString, QStringList> ParsexmlIns::getRoot_map() const
{
    return this->Root_map;
}

map<int, QStringList> ParsexmlIns::getRes_Map() const
{
    return this->Res_Map;
}

map<QString, QString> ParsexmlIns::getTemPath_Map() const
{
    return this->TemPath_Map;
}

QString ParsexmlIns::getname() const
{
    return _name;
}

void ParsexmlIns::setxmlcontent(QString &xmlcontent)
{
    this->xmlcontent = xmlcontent;
}

QString ParsexmlIns::getxmlpath() const
{
    return xmlpath;
}

QString ParsexmlIns::getxmlcontent() const
{
    return this->xmlcontent;
}

void ParsexmlIns::setcontentsToVector()
{
    QRegularExpression regex(MATCH_CNT);
    QRegularExpressionMatchIterator matchIterator = regex.globalMatch(xmlcontent);
    while(matchIterator.hasNext()){
        QRegularExpressionMatch match = matchIterator.next();
        if (match.hasMatch()) {
            QString content = match.captured(1);
//            qDebug() << content;
            contents.push_back(content);
        }
    }
}

void ParsexmlIns::handlecontentsToMap()
{
    for(auto & content : contents){
        bool flag = false;
        QString header = "";
        QString tail = "";
        for(auto & ch : content){
            if(!flag){
                if(ch == '{') flag = true;
                else
                    header += ch;
            }
            else{
                tail += ch;
            }
        }
        if(tail.length() >= 1)
            tail = tail.left(tail.length() - 1);
//        qDebug() << header;
        content_Map[header].push_back(tail);
    }
}

void ParsexmlIns::handlemapToJsons()
{
    for(auto it = content_Map.begin(); it != content_Map.end(); ++it){
        QString res = "{";
        auto key = it->first;
//        qDebug() << key;
        auto value = it->second;
        key += "{";
        for(int i = 0; i < value.size(); ++i){
            key += value[i];
            if(i != value.size() - 1){
                key += ",";
            }
        }
        key += "}}";
        res += key;
//        qDebug() << res;
        Jsons.push_back(res);
    }
}

void ParsexmlIns::setxmlpath(QString &xmlpath)
{
    this->xmlpath = xmlpath;
}


